﻿using System.Runtime.Serialization;

namespace Eflong.MonitoringLib.MonitoringUnit
{
    [DataContract]
    public class MonitoringUnitState
    {
        public enum States
        {
            /// <summary>
            /// 开关量状态0
            /// </summary>
            State0 = 0 ,

            /// <summary>
            /// 开关量状态1
            /// </summary>
            State1 ,

            /// <summary>
            /// 开关量报警状态
            /// </summary>
            State2 ,

            /// <summary>
            /// 模拟量正常
            /// </summary>
            Normal = 0x10 ,

            /// <summary>
            /// 低于下限
            /// </summary>
            UnderLowerLimit ,

            /// <summary>
            /// 超上限
            /// </summary>
            OverUpperLimit ,

            /// <summary>
            /// 超断电值 OverBreakLimit,低于复电值 UnderRestLimit, 调校 OverTiming
            /// </summary>
            OverBreakLimit ,

            /// <summary>
            /// 低于复电值
            /// </summary>
            UnderRestLimit ,

            /// <summary>
            /// 未知
            /// </summary>
            UnknownState = 0x80 ,

            /// <summary>
            /// 调校
            /// </summary>
            OverTiming ,

            /// <summary>
            /// 数据不变化
            /// </summary>
            DataNoChanged ,

            /// <summary>
            /// 负漂
            /// </summary>
            Underflow = 0xfa ,

            /// <summary>
            /// 超量程
            /// </summary>
            Overflow = 0xfb ,

            /// <summary>
            /// 故障  Fault = 0xfc,  分站故障 FZFault = 0xfd,
            /// </summary>
            Fault = 0xfc ,

            /// <summary>
            /// 分站故障
            /// </summary>
            FZFault = 0xfd ,

            /// <summary>
            /// 断线
            /// </summary>
            Break = 0xfe ,

            /// <summary>
            /// 通信中断
            /// </summary>
            Halt = 0xff ,
        };

        public static bool IsStatesFault( States states )
        {
            return states >= States.UnknownState ? true : false;
        }

        public enum ChineseStates
        {
            状态0 = States.State0 , 状态1 = States.State1 , 状态2 = States.State2 ,
            正常 = States.Normal , 超下限 = States.UnderLowerLimit , 超上限 = States.OverUpperLimit ,
            超断电 = States.OverBreakLimit , 复电 = States.UnderRestLimit , 数据不变化 = States.DataNoChanged ,
            甲烷馈电异常 = 31 ,
            未知 = States.UnknownState , 调校 = States.OverTiming ,
            负漂 = States.Underflow , 超量程 = States.Overflow ,
            故障 = States.Fault , 分站故障 = States.FZFault ,
            断线 = States.Break , 通信中断 = States.Halt ,
        };

        public enum AnalogStates
        {
            正常 = States.Normal , 超下限 = States.UnderLowerLimit , 超上限 = States.OverUpperLimit ,
            断电 = States.OverBreakLimit , 复电 = States.UnderRestLimit ,
            闭锁 = 断电 , 报警 = 超上限 ,
            未知 = States.UnknownState , 不巡检 = 未知 ,
            调校 = States.OverTiming , 数据不变化 = States.DataNoChanged ,

            负漂 = States.Underflow , 超量程 = States.Overflow , 溢出 = States.Overflow ,
            断线 = States.Break , 中断 = States.Halt , 通信中断 = States.Halt ,

            故障 = States.Fault , 分站故障 = States.FZFault ,
            暂停 = 未知 , 馈电异常 = 报警
        };

        /*
                public enum NewStates
                {
                    开关量正常 = 0, 开关量异常 = 1, 开关量报警 = 2, 断电馈电异常 = 3,
                    模拟量正常 = 16, 超下限 = 17, 超上限 = 18, 超断电 = 19, 断电 = 19, 复电 = 20,
                    调校 = 30, 甲烷馈电异常 = 31,
                    负漂 = 0xfa, 溢出 = 0xfb, 超量程 = 溢出,
                    故障 = 0xfc, 分站故障 = 0xfd, 断线 = 0xfe, 通信中断 = 0xff,
                    未知 = 0x80, 不巡检 = 0x81
                };*/

        public enum DigitalStates
        {
            态0 = States.State0 , 态1 = States.State1 , 态2 = States.State2 ,
            关 = States.State1 , 停 = States.State1 ,
            开 = States.State0 , 正向开 = States.State0 , 反向开 = States.State0 ,
            交流供电 = States.State0 , 电池供电 = States.State1 ,
            正常 = States.State0 , 报警 = States.State1 , 异常 = States.State1 ,
            吸合 = States.State0 , 断开 = States.State1 , 断电 = States.State2 ,
            开关量输入 = -1 , 继电器输出 = -2 , 数据不变化 = States.DataNoChanged ,
            中断 = States.Halt , 断线 = States.Break , 离线 = States.Break
        };

        /*
         * 正常 = 0, 报警 = 1, 断电 = 2,
        复电 = 4, 馈电异常 = 8, 调校 = 16,
        超量程 = 32, 分站故障 = 64, 不巡检 = 128,
        暂停 = 256, 传感器故障 = 512, 负漂 = 1024, 闭锁 = 2048
         *
         * 传感器故障	暂停	不巡检	分站故障	超量程	调校	断电	报警
         *
         */

        public static bool IsFault( States state )
        {
            return ( state >= States.UnknownState ) ? true : false;
        }

        public static States GetEflongStates( string stateName )
        {
            States state = ( States )GetChineseStates( stateName );
            return state;
        }

        /// <summary>
        /// 获取中文状态名
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public static string GetChineseStateString( States state )
        {
            return ( ( MonitoringUnitState.ChineseStates )state ).ToString( );
        }

        public static ChineseStates GetChineseStates( string stateName )
        {
            ChineseStates s = ChineseStates.超上限;
            switch ( stateName )
            {
                case "超上限":
                    s = ChineseStates.超上限;
                    break;

                case "超下限":
                    s = ChineseStates.超下限;
                    break;

                case "超断电":
                    s = ChineseStates.超断电;
                    break;

                case "复电":
                    s = ChineseStates.复电;
                    break;

                case "调校":
                    s = ChineseStates.调校;
                    break;

                case "数据不变化":
                    s = ChineseStates.数据不变化;
                    break;

                case "负漂":
                    s = ChineseStates.负漂;
                    break;

                case "超量程":
                    s = ChineseStates.超量程;
                    break;

                case "断线":
                    s = ChineseStates.断线;
                    break;

                case "通信中断":
                    s = ChineseStates.通信中断;
                    break;

                case "故障":
                    s = ChineseStates.故障;
                    break;

                case "未知":
                    s = ChineseStates.未知;
                    break;

                case "正常":
                    s = ChineseStates.正常;
                    break;

                case "状态0":
                    s = ChineseStates.状态0;
                    break;

                case "状态1":
                    s = ChineseStates.状态1;
                    break;

                case "状态2":
                    s = ChineseStates.状态2;
                    break;
            }
            return s;
        }

        public static AnalogStates GetAnalogState( string state )
        {
            AnalogStates s = AnalogStates.正常;
            switch ( state )
            {
                case "正常":
                    s = AnalogStates.正常;
                    break;

                case "报警":
                    s = AnalogStates.报警;
                    break;

                case "超下限":
                    s = AnalogStates.超下限;
                    break;

                case "超上限":
                    s = AnalogStates.超上限;
                    break;

                case "断电":
                case "超断电":
                    s = AnalogStates.断电;
                    break;

                case "复电":
                    s = AnalogStates.复电;
                    break;

                case "调校":
                case "标定数据":
                    s = AnalogStates.调校;
                    break;

                case "数据不变化":
                    s = AnalogStates.数据不变化;
                    break;

                case "负漂":
                    s = AnalogStates.负漂;
                    break;

                case "超量程":
                case "溢出":
                    s = AnalogStates.超量程;
                    break;

                case "断线":
                    s = AnalogStates.断线;
                    break;

                case "分站故障":
                    s = AnalogStates.分站故障;
                    break;

                case "故障":
                case "传感器故障":
                    s = AnalogStates.故障;
                    break;

                case "不巡检":
                    s = AnalogStates.不巡检;
                    break;

                case "暂停":
                    s = AnalogStates.暂停;
                    break;

                case "中断":
                    s = AnalogStates.中断;
                    break;

                case "馈电异常":
                    s = AnalogStates.馈电异常;
                    break;

                case "闭锁":
                    s = AnalogStates.闭锁;
                    break;
            }

            return s;
        }

        public static DigitalStates GetDigitalState( string state , MonitoringUnitType.Types type )
        {
            if ( state == "断线" || state == "中断" )
            {
                return DigitalStates.断线;
            }
            DigitalStates s = GetDigitalStates( state );

            string name = type.ToString( );
            switch ( name )
            {
                case "风机":
                case "主扇":
                case "局扇":
                case "风筒":
                case "断电器":
                case "断电":
                case "馈电器":
                case "馈电":
                case "闭锁":
                case "继电器":
                case "界闭":
                case "分站":
                    if ( s == DigitalStates.态0 )
                    {
                        return DigitalStates.态0;
                    }
                    else
                    {
                        return DigitalStates.态2;
                    }
                case "风门":
                case "烟雾":

                    //
                    if ( s == DigitalStates.态0 )
                    {
                        return DigitalStates.态2;
                    }
                    else
                    {
                        return DigitalStates.态0;
                    }
                default:
                    if ( s == DigitalStates.态0 )
                    {
                        return DigitalStates.态0;
                    }
                    else
                    {
                        return DigitalStates.态2;
                    }
            }

            return s;
        }

        public static DigitalStates GetDigitalStates( string state )
        {
            switch ( state )
            {
                case "关":
                case "停":
                case "异常":
                case "断开":
                case "直流供电":
                case "直流":
                case "报警":
                case "断电":
                case "有烟":
                    return DigitalStates.态1;
                case "开":
                case "正向开":
                case "反向开":
                case "正常":
                case "吸合":
                case "交流供电":
                case "交流":
                case "无烟":
                    return DigitalStates.态0;
            }
            return DigitalStates.断线;
        }
    }
}